package com.ddz.util;

import com.ddz.model.enums.SystemEnums;

import java.sql.*;

public class JDBCUtil {
    // 数据库连接URL
    private static final String DB_URL =
            "jdbc:mysql://" + SystemEnums   .IP.getValue() + ":" + SystemEnums.SQL_PORT.getValue() + "/" + SystemEnums.SQL_DATABASE.getValue() +
                    "?useSSL" +
                    "=false&serverTimezone=UTC";
    // 数据库用户名
    private static final String DB_USER = SystemEnums.SQL_ROOT_USERNAME.getValue();
    // 数据库密码
    private static final String DB_PASSWORD = SystemEnums.SQL_ROOT_PASSWORD.getValue();

    // 加载数据库驱动
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动加载失败: " + e.getMessage());
        }
    }

    /**
     * 获取数据库连接
     *
     * @return Connection 对象
     * @throws SQLException SQL 异常
     */
    public static Connection getConnection() throws SQLException {
        return DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
    }


    /**
     * 通用更新操作（INSERT、UPDATE、DELETE）
     *
     * @param sql    SQL 语句
     * @param params 可变参数
     * @return 影响的行数
     */
    public static int executeUpdate(String sql, Object... params) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            System.out.println(pstmt.toString());
            return pstmt.executeUpdate();
        } catch (SQLException e) {
            System.out.println("数据库更新操作失败:"+e.getMessage());
            return -1;
        } finally {
            closeResources(null, pstmt, conn);
        }
    }

    /**
     * 通用查询操作
     *
     * @param sql    SQL 语句
     * @param params 可变参数
     * @return ResultSet 结果集
     */
    public static ResultSet executeQuery(String sql, Object... params) {
        Connection conn;
        PreparedStatement pstmt;
        ResultSet rs;
        try {
            conn = getConnection();
            pstmt = conn.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
            rs = pstmt.executeQuery();
            return rs;
        } catch (SQLException e) {
            System.out.println("数据库查询操作失败: " + e.getMessage());
            return null;
        }
    }

    /**
     * 设置 PreparedStatement 的参数
     *
     * @param pstmt  PreparedStatement 对象
     * @param params 可变参数
     * @throws SQLException SQL 异常
     */
    private static void setParameters(PreparedStatement pstmt, Object... params) throws SQLException {
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                pstmt.setObject(i + 1, params[i]);
            }
        }
    }

    /**
     * 关闭资源（Connection, PreparedStatement, ResultSet）
     */
    public static void closeResources(ResultSet rs, PreparedStatement pstmt, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            System.out.println("关闭资源失败:" + e.getMessage());
        }
    }
}
